package dao

import (
	"fmt"
	"github.com/gomodule/redigo/redis"
)

const REQ_ID_KEY = "REQ_ID:"
func (d *Dao) Apply(bizType int32,step int) (reqId int64, err error) {
	key := fmt.Sprintf("%s%d",REQ_ID_KEY,bizType)
	conn := d.redis.Get()
	defer conn.Close()
	conn.Send("INCRBY",key,step)
	conn.Flush()
	reqId, err = redis.Int64(conn.Receive())

	if err != nil {
		reqId = -1
		fmt.Errorf("conn.Receive() error(%v)", err)
	}
	return
}

func (d *Dao) SaveReq(key string, json []byte) error{
	conn := d.redis.Get()
	defer conn.Close()
	conn.Send("SET",key,json)
	conn.Flush()
	_, err := redis.Bytes(conn.Receive())
	if err != nil {
		return err
	}
	return nil
}
func (d *Dao) GetReq(key string) (json []byte ,err error){
	conn := d.redis.Get()
	defer conn.Close()
	conn.Send("GET",key)
	conn.Flush()

	data, err := redis.Bytes(conn.Receive())
	if err != nil {
		return nil,err
	}
	return data,nil
}

//func (d *Dao) Apply (bizType int32,step int) (id int64, err error){
//	return
//}
func (d *Dao) MaxId (bizType int32) (id int64, err error) {
	return
}
